/* 产引入jsencrypt实现数据RSA加密 */
import JSEncrypt from 'jsencrypt' // 处理长文本数据时报错 jsencrypt.js Message too long for RSA
/* 产引入encryptlong实现数据RSA加密 */
import Encrypt from 'encryptlong' // encryptlong是基于jsencrypt扩展的长文本分段加解密功能。

import { Base64 } from 'js-base64';
// 公钥key
const publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCumsRv547qqpYNbUEMeyxvaHxks7WqRxxp3HV2Es/HFKqID4TodiWsqLlWEDDnx7WQllTC/QUt2343tsn9D0gtMp8yiTu3t21wEyQEJHJJoiytLEPetUVQkk6HEG9VULHWF81vwr6HK156q38bc1XE/g772sPVwEZ5irN7s2wOlQIDAQAB"
// 私钥key
const privateKey = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL3XMZeDJ2PrNUAmdi/MzNniwa9rqjiq76HRpwtkRWo1x2xLCkPAa3+IABAuhIToXM2jAco5GbHNP6isR20qTKzP9xW7X+K0N3zjbmL+ISjgWMCBYfSqDvL/UtUH6YfLoENJn4sPwZKoFi2KsLpbu2LLUIvVM1lIoGS/MEfS7F6ZAgMBAAECgYBxg0IY+YhNG7AsmpLixA/dXNQ5YyRWXAeLd8PtcNh8yIlk/H23w0/D4vs+scH1gsczAnwogfoWMccVwWW4JanQRXnh+bMpVIk7MLxTqZhZxBT0r3pow7AuxBAJCaQXyADUnp3tNShPzOwGiq/XuA/g9zv9rW8+RnkQkz8LBoTDbQJBAPrDZ2quzHXIMGjDJrCzeKdDRc5EY7ziip0NyvEn6SQlD6lNbUWN06N6Xl3sFFAncZq9LdPcc/EJ/MtVNYWDPEMCQQDBzhff460xNxyy2pvRl9De+PpyAK7g7MMwGWgE8MpJOOqAfvwRLWjeZJxdY51WcIwgfgMa+X350FT3Xci5//nzAkAaTUiZngAAEqFRO7bhLdHn6Vx3CCFV/IUBeCLjLIZ9rg14epCNjRS3vuwK+eofhiOaUWFvKV5X1+5V/XgBqwQ1AkEAsI/L3GMV0/J7qM3UizweRCTcNaQ9rhjFMjrTMz0TbWX27h3CEvwlwAY1Fje8jZV3c9qZhaeqhz6vSbUdO6bi1QJACSR9yg4NbSiuwLcvpFwpKexHdyUwDuHbtwHfZSv9v0//j6ty3/dfN2fWoSP5XEeGvx8k3i19aYMAlbbkjaU4MQ=='

export default {
    /* JSEncrypt加密 */
    rsaPublicData(data) {
      var jsencrypt = new JSEncrypt()
      jsencrypt.setPublicKey(publicKey)
      // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
      var result = jsencrypt.encrypt(data)
      return Base64.encode(result)
    },
    /* JSEncrypt解密 */
    rsaPrivateData(data) {
      var jsencrypt = new JSEncrypt()
      jsencrypt.setPrivateKey(privateKey)
      // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
      var result = jsencrypt.encrypt(Base64.decode(data))
      return result
    },
    /* 加密 */
    encrypt(data) {
      const PUBLIC_KEY = publicKey
      var encryptor = new Encrypt()
      encryptor.setPublicKey(PUBLIC_KEY)
      // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
      const result = encryptor.encryptLong(data)
      return result
    },
    /* 解密 - PRIVATE_KEY - 验证 */
    decrypt(data) {
      const PRIVATE_KEY = privateKey
      var encryptor = new Encrypt()
      encryptor.setPrivateKey(PRIVATE_KEY)
      // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
      var result = encryptor.decryptLong(data)
      return result
    }
  }